﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SIOsystem.Control;


namespace SIOsystem
{
    public partial class ReportForm : Form
    {
        public string uname = "";
        public long id = -1;
        public string rid = "";
        public ReportForm()
        {
            InitializeComponent();
        }

        private void InQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getInOrderList(InSD.Value, InED.Value, Inkeyword.Text);
            if (dt!=null)
            {
                OrderList.DataSource = dt;
                OrderList.Columns["单价"].DefaultCellStyle.Format = "N2";
                OrderList.Columns["金额"].DefaultCellStyle.Format = "N2";
                OrderList.Columns["入库日期"].DefaultCellStyle.Format = "M";
            }
        }

        private void ReportForm_Load(object sender, EventArgs e)
        {
            InSD.Value = InSD.Value.AddMonths(-1);
            OutSD.Value = OutSD.Value.AddMonths(-1);
            DeptSD.Value = DeptSD.Value.AddMonths(-1);
            ProductSD.Value = ProductSD.Value.AddMonths(-1);
            ApplySD.Value = ApplySD.Value.AddMonths(-1);
            DictionaryClass.fillDepartment(ref OutDept);
            DictionaryClass.fillDepartment(ref DeptList);
            DictionaryClass.fillMainClass(ref MainClass);
        }



/*
        private void ModInPrice_Click(object sender, EventArgs e)
        {
            if (OrderList.Rows.Count>0 && OrderClass.updateNewPrice(long.Parse(OrderList.SelectedRows[0].Cells["ISID"].Value.ToString())
                , double.Parse(NewPrice.Value.ToString()))==1)
            {
                MessageBox.Show("修改成功");
                DataTable dt = ReportClass.getInOrderList(InSD.Value.Date, InED.Value.AddDays(1).Date, Inkeyword.Text);
                if (dt!=null)
                {
                    OrderList.DataSource = dt;
                }
            }
        }*/

        private void OutQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getOutOrderList(OutSD.Value.Date, OutED.Value.AddDays(1).Date, OutKeyword.Text, OutName.Text, long.Parse(OutDept.SelectedValue.ToString()));
            if (dt!=null)
            {
                OutOrderList.DataSource = dt;
                OutOrderList.Columns["单价"].DefaultCellStyle.Format = "N2";
                OutOrderList.Columns["金额"].DefaultCellStyle.Format = "N2";
                OutOrderList.Columns["出库日期"].DefaultCellStyle.Format = "M";
            }
        }

        private void ProductQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getInOutDetail(ProductSD.Value.Date, ProductED.Value.AddDays(1).Date, long.Parse(MainClass.SelectedValue.ToString()));
            if (dt!=null)
            {
                InOutGridView.DataSource = dt;
            }
        }

        private void SumQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getDeptDetail(DeptSD.Value.Date, DeptED.Value.AddDays(1).Date
                                            , long.Parse(DeptList.SelectedValue.ToString()),SumMode.Checked);
            if (dt!=null)
            {
                DeptGridView.DataSource = dt;
            }
        }

        private void DeptSumExport_Click(object sender, EventArgs e)
        {
//             sfd.FileName = "工程材料进出库清单.xls";
//             if (sfd.ShowDialog() != DialogResult.OK) { return; }
//             DataTable dt = ReportClass.getDeptDetail(DeptSD.Value.Date, DeptED.Value.AddDays(1).Date, long.Parse(DeptList.SelectedValue.ToString()));
//             if (dt == null) { return; }
//             if (dt.Rows.Count==0) { return; }
//             dt.Columns[1].SetOrdinal(dt.Columns.Count - 1);
//             dt.Columns[0].ColumnName = "编号";
//             dt.Columns[1].ColumnName = "材料名称";
//             dt.Columns[2].ColumnName = "规格型号";
//             dt.Columns[3].ColumnName = "单位";
//             dt.Columns[4].ColumnName = "数量";
//             dt.Columns[5].ColumnName = "价格";
//             dt.Columns[6].ColumnName = "日期";
//             string[] strHeaderText = { "工程材料进出库清单", "（" + DeptSD.Value.Date.ToString("yy.MM.dd") + "-" + DeptED.Value.Date.ToString("yy.MM.dd") + "）", "" };
//             if (ExcelHelper.Export(dt, strHeaderText, sfd.FileName))
//             {
//                 MessageBox.Show("导出成功");
//             } 
//             else
//             {
//                 MessageBox.Show("导出失败，请先关闭已打开文件。");
//             }
        }

        private void ProductExport_Click(object sender, EventArgs e)
        {
            sfd.FileName = "工程材料进出库明细账.xls";
            if (sfd.ShowDialog() != DialogResult.OK) { return; }
            DataTable dt = ReportClass.getInOutDetail(ProductSD.Value.Date, ProductED.Value.AddDays(1).Date, long.Parse(MainClass.SelectedValue.ToString()));
            if (dt == null) { return; }
            if (dt.Rows.Count == 0) { return; }
            dt.Columns[1].SetOrdinal(dt.Columns.Count - 1);
            dt.Columns[0].ColumnName = "编号";
            dt.Columns[1].ColumnName = "材料名称";
            dt.Columns[2].ColumnName = "规格型号";
            dt.Columns[3].ColumnName = "单位";
            dt.Columns[4].ColumnName = "单价";
            dt.Columns[5].ColumnName = "上月盘存数";
            dt.Columns[6].ColumnName = "本月进货数";
            dt.Columns[7].ColumnName = "本月出货数";
            dt.Columns[8].ColumnName = "本月库存数";
            dt.Columns[9].ColumnName = "金额小计";
            string[] strHeaderText = { "工程材料进出库明细账", "（" + ProductSD.Value.Date.ToString("yy.MM.dd") + "-" + ProductED.Value.Date.ToString("yy.MM.dd") + "）", "" };
            if (ExcelHelper.Export(dt, strHeaderText, sfd.FileName))
            {
                MessageBox.Show("导出成功");
            }
            else
            {
                MessageBox.Show("导出失败，请先关闭已打开文件。");
            }
        }

        private void DeptSumPrint_Click(object sender, EventArgs e)
        {
            long PID = -1;
            if (DeptGridView.SelectedRows.Count == 1 && long.TryParse(DeptGridView.SelectedRows[0].Cells[0].Value.ToString(), out PID))
            {
                if (SumMode.Checked)
                {
                    ReportClass.PrintProjectSum(PID);
                } 
                else
                {
                    ReportClass.PrintProjectDetail(PID);
                }
            }

//             DataTable dt = ReportClass.getDeptDetail(DeptSD.Value.Date, DeptED.Value.AddDays(1).Date
//                                         , long.Parse(DeptList.SelectedValue.ToString()),SumMode.Checked);
//             if (dt == null) { return; }
//             if (dt.Rows.Count == 0) { return; }
//             dt.Columns[1].SetOrdinal(dt.Columns.Count - 1);
//             dt.Columns[0].ColumnName = "编号";
//             dt.Columns[1].ColumnName = "商品名称";
//             dt.Columns[2].ColumnName = "规格型号";
//             dt.Columns[3].ColumnName = "单位";
//             dt.Columns[4].ColumnName = "数量";
//             dt.Columns[5].ColumnName = "价格";
//             dt.Columns[6].ColumnName = "日期";
//             PrintHelper ph = new PrintHelper(dt);
//             ph.Print();
        }

        private void ProductPrint_Click(object sender, EventArgs e)
        {
//             DataTable dt = ReportClass.getInOutDetail(ProductSD.Value.Date, ProductED.Value.AddDays(1).Date, long.Parse(MainClass.SelectedValue.ToString()));
//             if (dt == null) { return; }
//             if (dt.Rows.Count == 0) { return; }
//             dt.Columns[1].SetOrdinal(dt.Columns.Count - 1);
//             dt.Columns[0].ColumnName = "编号";
//             dt.Columns[1].ColumnName = "商品名称";
//             dt.Columns[2].ColumnName = "规格型号";
//             dt.Columns[3].ColumnName = "单位";
//             dt.Columns[4].ColumnName = "单价";
//             dt.Columns[5].ColumnName = "上月盘存数";
//             dt.Columns[6].ColumnName = "本月进货数";
//             dt.Columns[7].ColumnName = "本月出货数";
//             dt.Columns[8].ColumnName = "本月库存数";
//             dt.Columns[9].ColumnName = "金额小计";
//             PrintHelper ph = new PrintHelper(dt);
//             ph.Print();
        }

        private void PrintOrder_Click(object sender, EventArgs e)
        {
            long IOID = -1;
            if (OrderList.SelectedRows.Count == 1 && long.TryParse(OrderList.SelectedRows[0].Cells[0].Value.ToString(), out IOID))
            {
                ReportClass.PrintInOrder(IOID);
            }
        }

        private void PrintOutOrder_Click(object sender, EventArgs e)
        {
            long OOID = -1;
            if (OutOrderList.SelectedRows.Count == 1 && long.TryParse(OutOrderList.SelectedRows[0].Cells[0].Value.ToString(), out OOID))
            {
                ReportClass.PrintOutOrder(OOID);
            }
        }

        private void ApplyQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getApplyOrderList(ApplySD.Value, ApplyED.Value.AddDays(1).Date, ApplyKeyword.Text);
            if (dt!=null)
            {
                ApplyOrderList.DataSource = dt;
                ApplyOrderList.Columns["申购日期"].DefaultCellStyle.Format = "M";
            }

        }

        private void PrintApplyOrder_Click(object sender, EventArgs e)
        {
            long AOID = -1;
            if (ApplyOrderList.SelectedRows.Count == 1 && long.TryParse(ApplyOrderList.SelectedRows[0].Cells[0].Value.ToString(), out AOID))
            {
                ReportClass.PrintApplyOrder(AOID);
            }
        }

    }
}
